"""

read core table txt, merge lines and write processed table
"""
import pandas as pd
import re
import os

total_lines = 16310 - 1
reGeneEntry = re.compile(r"G\d+_\d+\t.*")

proc_lines = []
line_str = ""
i=0
with open(os.path.join('data','core_table_gene.txt')) as _txt_file:
    lines = _txt_file.readlines()

class LineEntry:
    reGeneEntry = re.compile(r"^G\d+_\d+")
    def __init__(self,lines:list,entries_num:int):
        self.lines = lines
        self.entries_num = entries_num
        self.line_starting_points = self.get_entry_starting_index()
        assert len(self.line_starting_points)==entries_num, f"starting points num: {len(self.line_starting_points)} != entries num: {entries_num}"
        self.line_ending_points = self.line_starting_points[1:]
        self.line_ending_points.append(None)
        self.proc_lines = []
        for i,s in enumerate(self.line_starting_points):
            self.proc_lines.append(
                self.join_line_between_start_and_end(s,self.line_ending_points[i])
            )

    def get_entry_starting_index(self):
        starting_index_list = []
        for i,l in enumerate(self.lines):
            mo = self.reGeneEntry.match(l)
            if mo:
                starting_index_list.append(i)
        return starting_index_list

    def join_line_between_start_and_end(self,s_ind,e_ind):
        lines = self.lines[s_ind:e_ind]
        lines = [l.strip() for l in lines]
        return " ".join(lines)

    def __getitem__(self, idx):
        start = self.line_starting_points[idx]
        end = self.line_ending_points[idx]
        corresponding_lines = self.lines[start:end]
        rst = "".join(corresponding_lines)
        rst = rst.strip()
        return rst

    def __len__(self):
        return len(self.line_starting_points)

line_proc = LineEntry(lines,total_lines)
rst = "\n".join(line_proc)
with open(os.path.join('data','core_table_gene_proc.txt'),'w',encoding='utf8') as _txt_writer:
    _txt_writer.write(rst)
